package com.javaweb.httpservlet3;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.stream.Stream;

public class RequestTestServlet extends HttpServlet {

//    protected void doGet(HttpServletRequest  request, HttpServletResponse response)
//        throws ServletException, IOException {
//        response.setContentType("Text/html");
//        PrintWriter out = response.getWriter();
//        out.print(request);//org.apache.catalina.connector.RequestFacade@243e7c04
//    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //尝试使用request中的方法
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String[] habbits = request.getParameterValues("Habbits");
        out.print(username + "<br>");
        out.print(password + "<br>");
        Stream.of(habbits)
                .forEach(out::println);
        out.print("<br><hr>");

        Map<String,String[]> parameterMap = request.getParameterMap();
        //遍历Map集合
        parameterMap.entrySet()
                .stream()
                .forEach(entry->{
                    out.print(entry.getKey() + ": ");
                    Arrays.stream(entry.getValue())
                        .forEach(s->out.print(s + " "));
                    out.print("<br>");
                });
        out.print("<br><hr>");

        //直接通过getParameterNames()这个方法，可以直接获取这个Map集合的所有key
        Enumeration<String> names = request.getParameterNames();
        while(names.hasMoreElements()){
            String name = names.nextElement();
            String value = request.getParameter(name);
            out.print(name + ": " + value + "<br>");
        }
    }
}
